﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Drawing;

namespace RED_NET.ContentPage
{
    public partial class ValidateImage : System.Web.UI.Page
    {
        private readonly String ImageUrl = "~/Image_Css/validator.jpg";
        String tvalidator = "";
        Brush[] BrushList = new Brush[32];
        protected void Page_Load(object sender, EventArgs e)
        {
            //InitBrushList();
            if (Request.Params["Validator"] != null)
            {
                tvalidator = Request.Params["Validator"].ToString();//获取要书写的字符串
            }
            Bitmap bitMap = new System.Drawing.Bitmap(Server.MapPath(ImageUrl));//创建bmp位图
            Graphics graphics = Graphics.FromImage(bitMap);
            //设置画笔输出模式
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            //在位图上画上字符串
            for(int i=0;i<tvalidator.Length;i++)
            {
                graphics.DrawString(tvalidator[i].ToString(),
                    new Font("Arial", 20, (FontStyle)GetFrontStyleByInt(GetRandomInt(0, 1000))),
                    SystemBrushes.ControlDarkDark,
                    new PointF(i * 15, GetRandomInt(-5, 5)));
            }
            //设置图像输出格式
            Response.ContentType = "image/jpeg";
            //保存数据流
            bitMap.Save(Response.OutputStream,ImageFormat.Jpeg);
            //释放资源
            graphics.Dispose();
            bitMap.Dispose();
            Response.End();
        }



        //获取范围随机数
        private int GetRandomInt(int min,int max)
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());
            return(random.Next(min,max));
        }

        //获取随机的字体类型（整型表示）
        private int GetFrontStyleByInt(int random)
        {
            if (random < 200) return 0;
            else if (random < 400) return 1;
            else if (random < 600) return 2;
            else if (random < 800) return 3;
            else return 4;
        }

        /*
        /// <summary>
        /// 初始化Brush列表
        /// </summary>
        private void InitBrushList()
        {
            BrushList[0] = SystemBrushes.ActiveBorder;
            BrushList[1] = SystemBrushes.ActiveCaption;
            BrushList[2] = SystemBrushes.ActiveCaptionText;
            BrushList[3] = SystemBrushes.AppWorkspace;
            BrushList[4] = SystemBrushes.ButtonFace;
            BrushList[5] = SystemBrushes.ButtonHighlight;
            BrushList[6] = SystemBrushes.ButtonShadow;
            BrushList[7] = SystemBrushes.Control;
            BrushList[8] = SystemBrushes.ControlDark;
            BrushList[9] = SystemBrushes.ControlDarkDark;
            BrushList[10] = SystemBrushes.ControlLight;
            BrushList[11] = SystemBrushes.ControlText;
            BrushList[12] = SystemBrushes.Desktop;
            BrushList[13] = SystemBrushes.GradientActiveCaption;
            BrushList[14] = SystemBrushes.GradientInactiveCaption;
            BrushList[15] = SystemBrushes.GrayText;
            BrushList[16] = SystemBrushes.Highlight;
            BrushList[17] = SystemBrushes.HighlightText;
            BrushList[18] = SystemBrushes.HotTrack;
            BrushList[19] = SystemBrushes.InactiveBorder;
            BrushList[20] = SystemBrushes.InactiveCaption;
            BrushList[21] = SystemBrushes.InactiveCaptionText;
            BrushList[22] = SystemBrushes.Info;
            BrushList[23] = SystemBrushes.InfoText;
            BrushList[24] = SystemBrushes.Menu;
            BrushList[25] = SystemBrushes.MenuBar;
            BrushList[26] = SystemBrushes.MenuHighlight;
            BrushList[27] = SystemBrushes.MenuText;
            BrushList[28] = SystemBrushes.ScrollBar;
            BrushList[29] = SystemBrushes.Window;
            BrushList[30] = SystemBrushes.WindowFrame;
            BrushList[31] = SystemBrushes.WindowText;
        }
         * */
    }
}
